<?php namespace App\Models\Cxhshop;

use App\Models\Base\CxhshopBase;

class ShopMember extends CxhshopBase {
    protected $table = 'ims_ewei_shop_member';

    protected $fillable = ['*'];
    public $timestamps = false;
    protected $dates = [];

    public static $rules = [
        // Validation rules
    ];
    /**
     * 获取用户信息-根据uid
     * params1 String $uuid
     *  params1 Array $fields
     */
    public function getMemInfo($uuid,$fields=['*']){
        $query = self::query();
        $memInfo = $query -> where("member_uuid",$uuid)->first($fields);
        if($memInfo){
            $data = $memInfo ->toArray();
        }else{
            $data = [];
        }
        return $data;
    }

    /**
     * @param int $id
     * @param $memSaveData 修改的用户信息
     * @return int
     */
    public function saveFields($id = 0,$memSaveData){
        $query = self::query();
        $res = $query -> where('id',$id)->update($memSaveData);
        return $res;
    }

    /**
     * 根据用户id获取用户信息
     * @param $memId
     * @param array $fields
     * @return array
     */
    public function getMemInfoById($memId,$fields = ['*']){
        $query = self::query();
        $memInfo = $query -> where("id",$memId)->first($fields);
        if($memInfo){
            $data = $memInfo -> toArray();
        }else{
            $data = [];
        }
        return $data;
    }

    /**
     * 修改密码（支付密码和登录密码）
     * @param $memInfo
     * @param $params
     * @return int
     */
    public function changePassword($memInfo,$params){
        $type = $params['type'];
        $oldLogPwd = $memInfo['pwd'];//原来的登录密码
        $oldPayPwd = $memInfo['pay_passwd'];//原来的支付密码
        $oldSalt = $memInfo['salt'];
        $oldInput = $params['old'];
        $newInput = $params['new'];
        if($type == 1){//修改登录密码
            //拿到原来的盐和输入的旧密码
            $oldInputSalt = md5($oldInput.$oldSalt);
            if($oldLogPwd != $oldInputSalt){
                return 2;
            }
            $salt = $memInfo['salt'];
            if (empty($salt)) {
                $salt = generate_salt(16);
            }
            $newInputRe = md5($newInput.$salt);
            $saveData = [
                'pwd' => $newInputRe,
                'salt' => $salt,
            ];
        }elseif($type == 2){//修改支付密码
            $oldInputMd5 = md5($oldInput);
            if($oldInputMd5 != $oldPayPwd){
                return 2;
            }
            $newInputRe = md5($newInput);
            $saveData = [
                'pay_passwd' => $newInputRe,
            ];
        }
        $query = self::query();
        $res = $query -> where('id',$memInfo['id']) -> update($saveData);
        return $res;
    }

    /**
     * 根据电话查找用户
     * @param $mobile
     * @return int
     */
    public function getListByMobile($mobile){
        $num = self::query() -> where('mobile',$mobile) -> count();
        return $num;
    }

    /**
     * 根据上级的信息（id，电话，cxh账号）
     * @param $pid
     * @return array
     */
    public function getParentByPid($pid,$params = ["*"]){
        $pInfo = self::query() -> where('id',$pid) -> orWhere('cxhname',$pid) -> orWhere('mobile',$pid) -> first($params);
        if($pInfo){
            $data = $pInfo -> toArray();
        }else{
            $data = [];
        }
        return $data;
    }

}